package com.myjob.controller;

import com.alibaba.fastjson.JSONObject;
import com.myjob.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * @author Chen Yuan
 * @create 2021-03-18  14:41
 */
@RestController
@Slf4j
public class TestController {

	@GetMapping("/test")
	@PreAuthorize("hasAuthority('createAdvertisement')")
	public String test(@RequestHeader Map<String, String> headers,
	                   HttpServletRequest httpServletRequest) throws UnsupportedEncodingException {
		//headers.forEach((key, value) -> {
		//	// 日志中输出所有请求头
		//	log.info(String.format("Header '%s' = %s", key, value));
		//});
		String userStr = httpServletRequest.getHeader("user");
		userStr = URLDecoder.decode(userStr, "UTF-8");
		log.info("--- user --- "+ userStr);
		JSONObject userObject = JSONObject.parseObject(userStr);
		log.info("--- user --- "+ userObject);
		User user = JSONObject.parseObject(userStr, User.class);
		log.info("--- user --- "+ user);


		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		log.info(String.valueOf(authentication));

		log.info(String.valueOf(authentication.getDetails()));
		log.info(String.valueOf(authentication.getPrincipal()));
		log.info(String.valueOf(authentication.getAuthorities()));
		log.info(String.valueOf(authentication.getCredentials()));


		log.info(String.valueOf(authentication.isAuthenticated()));

		return "aaa";
	}
}
